home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / NeXTtext / text / ix.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  11.5 KB  |  336 lines

  1.  
  2. #ifndef _IX_INCLUDED_
  3. #define _IX_INCLUDED_
  4.  
  5. #import <stdio.h>
  6. #import <mach.h>
  7. #import <streams/streams.h>
  8. #import "btree/BTreeFile.h"
  9.  
  10. #define ixMaxKeyLength    255
  11. #define ixMinKeyLength    2
  12. #define ixMaxRegExps    512
  13.  
  14. // enumeration of error codes reported
  15. typedef enum {
  16.     ixErrorNoError,     // no error: the normal state of affairs
  17.     ixErrorNoMemory,     // sufficient memory is not available
  18.     ixErrorInternal,     // an internal inconsistency was found
  19.     ixErrorSystem = 100    // base value added to system variable errno
  20. } ixErrorType;
  21.  
  22. typedef struct {
  23.     unsigned short    regexCount;
  24.     struct regex    *regexTable[ixMaxRegExps];
  25. } ixRegexTable;
  26.  
  27. typedef struct {
  28.     float        threshold;
  29.     float        percentage;
  30.     unsigned char    absolute;
  31.     unsigned char    peculiar;
  32.     unsigned char    discontract;
  33.     unsigned char    foldToLower;
  34.     unsigned char    foldPlurals;
  35.     unsigned long    minCount;
  36.     unsigned long    maxWords;
  37.     unsigned long    windowShift;
  38.     unsigned long    windowSize;
  39.     WFTable        *wordDomain;
  40. } ixHistRecord;
  41.  
  42. typedef struct {
  43.     void        *userRecord;
  44.     unsigned char    writeOffsets;
  45.     unsigned char    writeUnknown;
  46.     unsigned char    dispunctuate;
  47.     void        (*controlWord)(char *, void *);
  48.     vm_offset_t    (*readOffset)(void *);
  49.     void        (*writeOffset)(vm_offset_t, void *);
  50.     int        (*charReader)(void *);
  51.     int        (*charWriter)(int, void *);
  52.     int        (*charReturn)(int, void *);
  53. } ixRTFRecord;
  54.  
  55. typedef struct FileCell {
  56.     char        *file;        // name of file referenced
  57.     char        *body;        // pointer to out-of-line data
  58.     char        *desc;        // a short human readable description
  59.     char        *type;        // the type of the object, if known
  60.     char        *regex;        // a regular expression for object
  61.     char        *message;    // a message to be sent for object
  62.     char        *data;        // a copy of the image of the FileCell
  63.     struct FileCell    *next;        // for creating a linked list
  64.     unsigned long    id;        // unique key and id number
  65.     unsigned long    pointer;    // a pointer to another FileCell
  66.     unsigned long    size;        // the size of the FileCell
  67.     unsigned long    date;        // the date the FileCell was written
  68.     unsigned long    mode;        // access modes at the time of writing
  69.     unsigned long    offset;        // offset of referenced object
  70.     unsigned long    length;        // length of referenced object
  71.     unsigned long    line;        // line no. of referenced object
  72.     unsigned long    bodysize;    // amount of data stored at 'body'
  73.     struct stat    statbuf;    // for saving the results of a 'stat'
  74.     char        status;        // the status of the FileCell
  75. } FileCell;
  76.  
  77.  
  78. // A Reference is a weighted pointer to a FileCell
  79. typedef struct Reference {
  80.     float        weight;
  81.     FileCell    *f;
  82. } Reference;
  83.  
  84. // A RefList is a list of FileCells, as returned by ixGetRefList
  85. typedef struct RefList {
  86.     unsigned long    n;
  87.     Reference    *r;
  88. } RefList;
  89.  
  90. // A Registration associates a FileCell and indexing date with a file name
  91. typedef struct Registration {
  92.     unsigned long    id;
  93.     unsigned long    date;
  94. } Registration;
  95.  
  96. // An Index is a descriptor providing access to an index.
  97. typedef struct Index {
  98.     char        *name;            // the name of the index file
  99.     char        *folder;        // the name of the folder
  100.     int        flag;            // a flag word for the index
  101.     BTreeCursor    *registryCursor;
  102.     BTreeCursor    *keywordsCursor;
  103.     BTreeCursor    *fileCellCursor;
  104.     BTreeFile    *btreeFile;        // a handle to the index file
  105. } Index;
  106.  
  107.  
  108. // flag bits used in the flag word of the index descriptor
  109. #define    ixFlagNewIndex        (1 << 0)    // new index under construction
  110. #define    ixFlagCreating        (1 << 1)    // during file creation process
  111.  
  112. // macro for closing an array of Index descriptors returned by ixOpenArray
  113. #define    ixCloseArray(ix)    while (*(ix)) ixClose(*(ix)++)
  114.  
  115. typedef enum {
  116.     ixSortByDate, ixSortByWeight, 
  117.         ixSortByDescription, ixSortByType, ixSortByName
  118. } ixSortMethod;
  119.  
  120. typedef enum {
  121.     ixSortReverse, ixSortForward
  122. } ixSortDirection;
  123.  
  124. typedef enum {
  125.     ixSearchByPrefix, ixSearchByFullWord, ixSearchAnywhere
  126. } ixSearchMethod;
  127.  
  128. typedef enum {
  129.     ixMatchFileName, ixMatchContent, ixMatchFileNameOrContent
  130. } ixMatchAgainst;
  131.  
  132. typedef enum {
  133.     ixLiteralString, ixRegularExpression
  134. } ixQuerySemantic;
  135.  
  136. extern ixErrorType    ixErrorNo;
  137. extern int        ixErrors;
  138. extern int        ixError(ixErrorType errno, char *fmt, ...);
  139.  
  140. // flags used to specify indexing characteristics
  141. extern char    *ixFileExtension;    // file extension for index files
  142. extern char    *ixStopWord;        // name of stop word list file
  143. extern char    *ixStopWordDefault;    // appended to /usr/lib/indexing/files
  144. extern int    ixIndexFileNames;    // if true, put file names in index
  145.  
  146. // indexing characteristics examined at query time
  147. extern int    ixFoldToLower;        // if true, fold keys to lower case
  148. extern int    ixFoldPlurals;        // if true, plural forms are folded
  149.  
  150. // flags used to specify querying characteristics
  151. extern int    ixNoCaseQuery;        // if true, query is case insensitive
  152. extern int    ixFindPlurals;        // if true, check plural and singular
  153.  
  154. // regular expression tables used by indexing and searching
  155. extern ixRegexTable _ixBadNameTable;
  156. extern ixRegexTable _ixGoodNameTable;
  157.  
  158. // routines for working with regular expression tables
  159. inline static int 
  160. _ixCheckRegexTable(char *itemString, ixRegexTable *regexTable)
  161. {
  162.     int    i;
  163.  
  164.     for (i = 0; i < regexTable->regexCount; ++i)
  165.         if (re_match_exactly(itemString, regexTable->regexTable[i]))
  166.             return 1;
  167.  
  168.     return 0;
  169. }
  170.  
  171. inline static int 
  172. _ixIsGoodName(char *fileName)
  173. {
  174.     return _ixGoodNameTable.regexCount ? 
  175.         _ixCheckRegexTable(fileName, &_ixGoodNameTable) : 
  176.             ! _ixCheckRegexTable(fileName, &_ixBadNameTable);
  177. }
  178.  
  179. extern int _ixItemToRegexTable(char *itemString, 
  180.     ixRegexTable *regexTable, int shellSyntax);
  181. extern int _ixFileToRegexTable(char *fileName, 
  182.     ixRegexTable *regexTable, int shellSyntax);
  183. extern int _ixPathToRegexTable(char *parentName, char *fileName, 
  184.     ixRegexTable *regexTable, int shellSyntax);
  185.  
  186. // routines for opening, closing, flushing and removing indices
  187. extern int _ixUnlink(char *name);
  188. extern int ixUnlink(char *name);
  189. extern Index *ixOpen(char *name, char *mode);
  190. extern Index **ixOpenArray(char **path, char *mode);
  191. extern Index *ixOpenParent(char *file);
  192. extern Index *ixCleanIndex(Index *ix, int verbose);
  193. extern void ixFlush(Index *ix);
  194. extern void ixClose(Index *ix);
  195.  
  196. // routines for managing FileCells
  197. extern void _ixLoadFileCell(unsigned long id, FileCell *fileCell);
  198. extern int _ixMergeFileCell(FileCell *fileCell, FileCell *tempFileCell);
  199. extern FileCell *_ixReadFileCell(Index *ix, unsigned long id);
  200. extern FileCell *_ixReadFirstFileCell(Index *ix);
  201. extern FileCell *_ixReadNextFileCell(Index *ix);
  202. extern int _ixWriteFileCell(Index *ix, FileCell *fileCell);
  203. extern FileCell *ixCopyFileCell(FileCell *fileCell, char *file);
  204. extern FileCell *ixFreeFileCell(FileCell *fileCell);
  205. extern FileCell *ixInitFileCell(char *file, char *type, char *desc, 
  206.     char *message, char *regex, struct stat *b);
  207. extern FileCell *ixLoadFileCell(Index *ix, FileCell *fileCell);
  208. extern FileCell *ixMergeFileCell(FileCell *fileCell, FileCell *tempFileCell);
  209. extern FileCell *ixNewFileCell();
  210. extern FileCell *ixReadFileCell(Index *ix, unsigned long id);
  211. extern FileCell *ixReadFirstFileCell(Index *ix);
  212. extern FileCell *ixReadNextFileCell(Index *ix);
  213. extern int ixRemoveFileCell(Index *ix, FileCell *fileCell, int remove);
  214. extern int ixRemoveFileCellById(Index *ix, unsigned long id, int remove);
  215. extern int ixRemoveFileCellByName(Index *ix, char *name, int remove);
  216. extern int ixWriteFileCell(Index *ix, FileCell *fileCell);
  217. extern int ixWriteStatus(Index *ix, FileCell *fileCell, char status);
  218.  
  219. // routines for managing FileCell registration
  220. extern Registration *ixGetRegistration(Index *ix, char *name);
  221. extern unsigned long ixGetRegistrationId(Index *ix, char *name);
  222. extern FileCell *ixGetRegistrationFileCell(Index *ix, char *name);
  223. extern unsigned long ixGetRegistrationDate(Index *ix, char *name);
  224. extern int ixPutRegistration(Index *ix, FileCell *f);
  225. extern int ixClearRegistrationByName(Index *ix, char *name);
  226. extern int ixClearRegistration(Index *ix, FileCell *f);
  227.  
  228. // routines for printing the contents of FileCells and indices
  229. extern void ixPrintFileCell(FileCell *f, float weight);
  230. extern void ixPrintFileCellById(Index *ix, unsigned long id, int pointerFlag);
  231. extern void ixPrintIndexQuery(Index *ix, char *queryString, 
  232.     ixSearchMethod searchBy, ixMatchAgainst matchAgainst, 
  233.     ixQuerySemantic querySemantic, 
  234.         ixSortMethod sortBy, ixSortDirection sortDirection);
  235. extern void ixPrintSearchQuery(char *applyFolder, char *queryString, 
  236.     ixSearchMethod searchBy, ixMatchAgainst matchAgainst, 
  237.     ixQuerySemantic querySemantic, 
  238.         ixSortMethod sortBy, ixSortDirection sortDirection);
  239. extern void ixPrintRefList(Index *ix, char *s, ixSearchMethod searchBy, 
  240.     ixSortMethod sortBy, ixSortDirection sortDirection);
  241.  
  242. // routines for managing the string matching tables
  243. typedef NXHashTable    *ixMatchTable;
  244.  
  245. extern ixMatchTable    _ixStopWordTable;
  246. extern ixMatchTable    _ixBadTypeTable;
  247. extern ixMatchTable    _ixGoodTypeTable;
  248. extern ixMatchTable    _ixTextTypeTable;
  249.  
  250. inline static int 
  251. _ixItemToMatchTable(char *itemString, ixMatchTable *matchTable)
  252. {
  253.     if (! *matchTable && 
  254.         ! (*matchTable = NXCreateHashTable(NXStrPrototype, 64, 0)))
  255.         return 0;
  256.  
  257.     return ! NXHashInsert(*matchTable, strsave(itemString));
  258. }
  259.  
  260. inline static int 
  261. _ixCheckMatchTable(char *itemString, ixMatchTable *matchTable)
  262. {
  263.     return *matchTable ? NXHashMember(*matchTable, itemString) : 0;
  264. }
  265.  
  266. inline static int 
  267. _ixIsGoodType(char *fileType)
  268. {
  269.     return _ixGoodTypeTable ? NXHashMember(_ixGoodTypeTable, fileType) : 
  270.         (!_ixBadTypeTable || !NXHashMember(_ixBadTypeTable, fileType));
  271. }
  272.  
  273. extern int _ixFileToMatchTable(char *fileName, ixMatchTable *matchTable);
  274. extern int _ixPathToMatchTable(char *parentName, 
  275.     char *fileName, ixMatchTable *matchTable);
  276. extern void ixLoadStopWordTable(void);
  277.  
  278. #define _ixLoadStopWordTable(fileName) \
  279.     _ixFileToMatchTable(fileName, &_ixStopWordTable)
  280.  
  281. #define    _ixStopWordEnter(stopWord) \
  282.     _ixItemToMatchTable(stopWord, &_ixStopWordTable)
  283.  
  284. inline static const char 
  285. *_ixStopWordCheck(const char *stopWord)
  286. {
  287.     return (const char *) (_ixStopWordTable ?
  288.         NXHashGet(_ixStopWordTable, stopWord) : 0);
  289. }
  290.  
  291. inline static void 
  292. _ixClearStopWordTable(void)
  293. {
  294.     if (_ixStopWordTable)
  295.         NXResetHashTable(_ixStopWordTable);
  296. }
  297.  
  298. // routines for testing and creating keyword/FileCell associations
  299. extern int _ixAssociate(Index *ix, char *k, unsigned long id, float w);
  300. extern unsigned long ixAssociate(Index *ix, char *k, 
  301.     unsigned long id, float w);
  302. extern int ixAssociated(Index *ix, char *s, unsigned long id);
  303. extern unsigned long ixAddFileCell(Index *ix, NXStream *keys, FileCell *f);
  304.  
  305. // routines for getting, clearing and sorting RefLists
  306. extern int ixOrRefList(RefList *sourceList, RefList *targetList);
  307. extern int ixAndRefList(RefList *sourceList, RefList *targetList);
  308. extern int ixAndNotRefList(RefList *sourceList, RefList *targetList);
  309. extern void ixSortRefList(RefList *refList, 
  310.         ixSortMethod sortBy, ixSortDirection sortDirection);
  311. extern RefList *ixClearRefList(RefList *refList);
  312. extern RefList *ixGetRefList(Index *ix, char *queryString, 
  313.     RefList *refList, ixSearchMethod searchBy, int (*interrupt)());
  314. extern RefList ixIndexQuery(Index *ix, char *queryString, 
  315.     ixSearchMethod searchBy, ixMatchAgainst matchAgainst, 
  316.         ixQuerySemantic querySemantic, int (*interrupt)());
  317. extern RefList ixSearchQuery(char *applyFolder, char *queryString, 
  318.     ixSearchMethod searchBy, ixMatchAgainst matchAgainst, 
  319.         ixQuerySemantic querySemantic, int (*interrupt)());
  320.  
  321. // routines for managing file names and paths
  322. extern char *ixParentFolder(Index *ix);
  323. extern char *_ixFromParentFolder(Index *ix, char *file);
  324. extern char *ixFullPath(Index *ix, char *file);
  325.  
  326. // routines for filtering rich text and generating histograms
  327. extern int ixHistogram(NXStream *inputStream, 
  328.     NXStream *outputStream, ixHistRecord *histRecord);
  329. extern void _ixReadRichText(ixRTFRecord *rtfRecord);
  330. extern void ixReadRichText(NXStream *inputStream, 
  331.     NXStream *outputStream, unsigned long outputLimit);
  332.  
  333.  
  334. #endif
  335.  
  336.